package com.it.controller;

import com.alibaba.fastjson.JSONObject;
import com.it.dao.SingerMapper;
import com.it.dao.TagMapMapper;
import com.it.domain.Singer;
import com.it.service.SingerService;
import com.it.common.CONSTS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/Singer")
@RestController
public class SingerController {

    @Autowired
    private SingerService singerService;

    @Autowired
    private SingerMapper singerMapper;

    @Autowired
    private TagMapMapper tagMapMapper;

    @PostMapping("/AddSinger")
    public Object AddSinger(@RequestBody Singer singer) {


        String name = singer.getName().trim();
        Byte sex = singer.getSex();
        String image = singer.getImage().trim();
        String location = singer.getLocation().trim();
        String introduction = singer.getIntroduction().trim();
        Date birth = singer.getBirth();
        Singer singer1 = new Singer();
        singer1.setImage(image);
        singer1.setBirth(birth);
        singer1.setLocation(location);
        singer1.setSex(sex);
        singer1.setName(name);
        singer1.setIntroduction(introduction);

        return singerService.AddSinger(singer1);
    }

    @PutMapping("/UpdateSinger")
    public Object UpdateSinger(@RequestBody Singer singer){

        tagMapMapper.DeleteTagMap(singer.getId());
        JSONObject jsonobject = new JSONObject();
        Integer id = singer.getId();
        String name = singer.getName().trim();
        Byte sex = singer.getSex();
        String image = singer.getImage().trim();
        String location = singer.getLocation().trim();
        String introduction = singer.getIntroduction().trim();
        Date birth = singer.getBirth();

        Singer singer1 = new Singer();
        singer1.setImage(image);
        singer1.setBirth(birth);
        singer1.setLocation(location);
        singer1.setSex(sex);
        singer1.setName(name);
        singer1.setIntroduction(introduction);
        singer1.setId(id);

        boolean flag = singerService.updateSinger(singer1);

        if (flag) {
            jsonobject.put(CONSTS.CODE, 200);
            jsonobject.put(CONSTS.MSG, "更新成功");
        } else {
            jsonobject.put(CONSTS.CODE, 400);
            jsonobject.put(CONSTS.MSG, "更新失败");
        }
        return jsonobject;
    }

    @DeleteMapping("/deleteSinger")
    public Object DeleteSinger(@RequestParam Integer id){
        JSONObject jsonObject = new JSONObject();
        boolean flag = singerService.deleteSinger(id);
        if (flag) {
            jsonObject.put(CONSTS.CODE, 200);
            jsonObject.put(CONSTS.MSG, "删除成功");
        } else {
            jsonObject.put(CONSTS.CODE, 500);
            jsonObject.put(CONSTS.MSG, "删除失败");
        }
        return jsonObject;
    }

    @GetMapping("/{id}")
    public Singer SelectByid(@PathVariable Integer id){
        return singerService.SelectBid(id);
    }

    @PostMapping("/AllSinger")
    public Map<String,Object> SelectAll(@RequestParam("PageNum") Integer PageNum, @RequestParam("PageSize") Integer PageSize, @RequestParam("name") String name){
        System.out.println(PageNum);
        System.out.println(PageSize);
        name = '%' + name + '%';
        Map<String,Object> res = new HashMap<String,Object>();
        PageNum = (PageNum-1)*PageSize;
        Integer total = singerService.SelectTotal();
        List<Singer> data =  singerService.SelectAll(PageNum,PageSize,name);
        res.put("total",total);
        res.put("data",data);
        System.out.println(res);
        return res;
    }

    @PostMapping("/getAll")
    public List<Singer> SelectByName(@RequestParam String name) {
        name = '%' + name + '%';
        return singerService.SelectByName(name);
    }

    @GetMapping("/GetAll")
    public List<Singer> GetAll(){
        return singerService.GetAll();
    }

    @PostMapping("/GetSingerId")
    public Singer GetSingerId(@RequestBody Singer singer){
        String name = singer.getName().trim();
        Byte sex = singer.getSex();
        return singerMapper.SelectIsExist(name,sex);
    }

    @PostMapping("/SelectIsExist")
    public Singer SelectIsExist(@RequestParam String name,@RequestParam Byte sex){
        return singerMapper.SelectIsExist(name,sex);
    }

    @PostMapping("/getSingerLists")
    public List<Singer> getSingerLists(@RequestParam Integer[] ids){
        if(ids == null){
            return null;
        }else{
            return singerService.getSingerLists(ids);
        }
    }

    @GetMapping("/SelectTotalSinger")
    public Integer SelectTotalSinger(){
        return singerService.SelectTotal();
    }
}
